﻿using System.Collections.Generic;

namespace Core
{
    public class PrefixDict
    {
        private readonly string prefix;
        private readonly bool ignoreSuffix;

        private readonly Dictionary<string, string> dict = new();

        public PrefixDict(string prefix, bool ignore_suffix = false)
        {
            this.prefix = prefix;
            ignoreSuffix = ignore_suffix;
        }

        public string this[string key]
        {
            get
            {
                if (dict.TryGetValue(key, out var item)) return item;

                if (key.StartsWith(prefix) == false)
                {
                    dict[key] = $"{prefix}{key}";
                }
                else
                {
                    dict[key] = key;
                }

                if (!ignoreSuffix) return dict[key];

                var value = dict[key];
                var index = value.LastIndexOf('.');
                if (index >= 0)
                {
                    value = value[..index];
                }

                dict[key] = value;
                return dict[key];
            }
        }

        public void Clear()
        {
            dict.Clear();
        }
    }
}
